//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//
//                          @王帅
package com.basics.controller;

import javax.servlet.http.HttpServletRequest;

import com.basics.dto.CuxUsers;
import com.basics.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户controller层
 * @author shuaiwang02@hand-china.com
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;


    /**
     * 用户登录.
     * @param request 请求上下文
     * @return 返回对应页面
     */
    @RequestMapping("/login")
    public String selectUser(HttpServletRequest request)  {

        String userName = request.getParameter("userName");
        String password = request.getParameter("passWord");

        CuxUsers user = this.userService.selectUser(userName,password);
        if (user!=null){
            HttpSession session = request.getSession();
            session.setAttribute("SESSION_USER", user);
            return "main";
        }
       return "error";
    }


    /**
     *  返回main页面
     * @return ResponseData ResponseData
     */
    @RequestMapping("/main")
    public String returnMain(HttpServletRequest request, HttpServletResponse response)  {

        return "main";
    }

    /**
     * 返回个人信息infomation页面
     * @return ResponseData ResponseData
     */
    @RequestMapping("/infomation")
    public String returnInfomation()  {

        return "infomation";
    }

    /**
     * 根据用户ID查询个人信息.
     * @return 返回个人信息user类
     */
    @RequestMapping("/selectUserInfomationById")
    @ResponseBody
    public CuxUsers selectUserInfomationById(HttpServletRequest request) {

        HttpSession session = request.getSession();
        CuxUsers loggedUser =  (CuxUsers) session.getAttribute("SESSION_USER");
        CuxUsers user = userService.selectUserInfomationById(loggedUser.getUserId());
        return user;
    }

    /**
     * 根据用户ID更新个人信息.
     * @return 返回影响条数
     */
    @RequestMapping("/updateInfomationByUserId")
    @ResponseBody
    public Integer updateInfomationByUserId(HttpServletRequest request) {

        HttpSession session = request.getSession();
        CuxUsers loggedUser =  (CuxUsers) session.getAttribute("SESSION_USER");
        CuxUsers user = new CuxUsers();
        user.setUserId(loggedUser.getUserId());
        user.setUserName(request.getParameter("user_name"));

        if (request.getParameter("pass_word")!=""){
            user.setPassword(request.getParameter("pass_word"));
        }
        user.setSex(request.getParameter("sex"));
        user.setAge(request.getParameter("age"));
        user.setPhoneNumber(request.getParameter("phone"));
        user.setComments(request.getParameter("comments"));
        Integer num = userService.updateInfomationByUserId(user);
        return num;
    }

}